package com.vmix.android.vmixsdk.rds;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.vmix.android.vmixsdk.valueobject.VmixVideo;
import com.vmix.android.vmixsdk.valueobject.VmixVideos;

import android.os.AsyncTask;
import org.w3c.dom.Element;


public class GetMediaListAsyncTask extends AsyncTask<String, Void, VmixVideos> {
	
	@Override
	protected VmixVideos doInBackground( String...urls ) {
		
		VmixVideos videos = new VmixVideos();
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		try {
			
			DocumentBuilder builder = factory.newDocumentBuilder();
			String mediaServiceUrl = urls[0];
			
			Document dom = builder.parse( mediaServiceUrl );
			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName( "media" );
			
			for( int i = 0; i < items.getLength(); i++ ) {

				VmixVideo video = new VmixVideo();
				
				// get media item
				Node mediaItem = items.item(i);
				NodeList properties = mediaItem.getChildNodes();
				
				// get a list of the childnodes
				for( int j = 0; j < properties.getLength(); j++ ) {
					
					// pull out property name and value
					Node property = properties.item(j);
					String propertyName = property.getNodeName();
					String propertyValue = property.getNodeValue();
					
					// set id
					if( propertyName.equalsIgnoreCase("id") ) {
						video.setMediaId(propertyName);
					}
					// set partner id
					else if( propertyName.equalsIgnoreCase("partner_id") ) {
						video.setPartnerId(propertyName);
					}
					// set title
					else if( propertyName.equalsIgnoreCase("title")) {
						video.setTitle(propertyValue);
					}
					// set description
					else if( propertyName.equalsIgnoreCase("description") ) {
						video.setDescription(propertyName);
					}
					// set class_id
					else if( propertyName.equalsIgnoreCase("class_id") ) {
						video.setClassId(propertyName);
					}
					// set date created
					else if( propertyName.equalsIgnoreCase("date_created") ) {
						video.setDateCreated(propertyName);
					}
					// set date approved
					else if( propertyName.equalsIgnoreCase("date_approved") ) {
						video.setDateApproved(propertyName);
					}
					// set last modified
					else if( propertyName.equalsIgnoreCase("date_modified") ) {
						video.setLastModified(propertyName);
					}
					// set source name
					else if( propertyName.equalsIgnoreCase("source_name") ) {
						video.setSourceName(propertyName);
					}
					// set total views
					else if( propertyName.equalsIgnoreCase("total_views") ) {
						video.setTotalViews(propertyName);
					}

				}
				
				videos.add( video );
			}
			
			return videos;
		}
		catch( Exception e ) {
			
		}
		
		return null;
		
	}

	@Override
	protected void onPostExecute(VmixVideos result) {
		// TODO
		super.onPostExecute(result);
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		// TODO
		super.onProgressUpdate(values);
	}
	
}